<?php
namespace app\home\controller;
use \think\Db;
use \think\Cookie;
use \think\Session;
use \think\Cache;
use \think\Controller;
class Index extends Controller
{
    public function index()
    {
       
       return THINK_VERSION;
    }
   #添加解锁记录
    public function add_unlock()
    {
        db('user_pool')->where(['one_balance'=>0])->update(['status'=>2]);
        $time = date('Y-m-d H:i:s',time());
        $data = db('user_pool')->where(['unlock_date'=>['<=',$time],'status'=>1])->select();
        if(empty($data))return false;
        $config = db('config')->find()['pool_to_momey']/100;

        $insertAll = [];
        $log = [];
        $valid_value=db('config')->where(['id'=>1])->value('valid_value'); //最小数值
        Db::startTrans();
        try {
            foreach ($data as $key => $value) {
                $time1  = strtotime($time);
                $time2  = strtotime($value['unlock_date']);
                $day    = 24*60*60*30;
                // $day    = 24*60*60;
                $times  = (($time1-$time2-($time1-$time2)%$day)/$day)+1;
                if($times > $value['log_times']){
                    $lock = min($value['unlock_money'],$value['one_balance']);
                    if($lock > 0){
                        $money =  $lock* $config;
                        $insertAll[] = [ 
                            'uid'           =>  $value['uid'],
                            'pool_id'       =>  $value['id'],
                            'money'         =>  $money,
                            'balance'       =>  $lock - $money,
                            'create_time'   =>  date('YmdHis')
                        ];
                        //添加解锁次数次数//添加使用金额//减少剩余金额
                        db('user_pool')->where(['id'=>$value['id']])->update([
                            'log_times'     => $value['log_times'] + 1,
                            'use_money'     => $value['use_money'] + $money,
                            'one_balance'   => $value['one_balance'] - $money,
                            'unlock_money'  => $lock - $money
                        ]);
                        $user = db('users')->where(['id'=>$value['uid']])->find();
                        $log[] = [
                            'uid'         => $value['uid'],
                            'source_id'   => 0,                  
                            'money'       => $money,        
                            'balance'     => $user['activity_pool'] + $money,  
                            'message'     => "解鎖到活動賬戶", 
                            'type'        => 1, 
                            'class'       => 1,  
                            'status'      => 2,      
                            'create_time' => date('YmdHis')
                        ];
                        $log[] = [
                            'uid'         => $value['uid'],
                            'source_id'   => 0,                  
                            'money'       => $money,        
                            'balance'     => $user['pool'] - $money,    
                            'message'     => "解鎖到活動賬戶", 
                            'type'        => 2, 
                            'class'       => 2,  
                            'status'      => 2,      
                            'create_time' => date('YmdHis')
                        ];
                        
                        if($user['type'] == 2 && ($user['pool'] - $money) < $valid_value){
                            db('users')->where(['id'=>$value['uid']])->update([
                                    'activity_pool'     => $user['activity_pool'] + $money,
                                    'type'              => 1,
                                    'pool'              => $user['pool'] - $money
                                ]);
                            db('user_position')->where('uid',$this->userId)->update(['is_effective'=>1]);
                        }else{
                            db('users')->where(['id'=>$value['uid']])->update([
                                    'activity_pool'     => $user['activity_pool'] + $money,
                                    'pool'              => $user['pool'] - $money
                                ]);
                        }       
                        

                    }
                }
            }
            //解锁日志
            if($insertAll)db('unlock')->insertAll($insertAll);
            //解锁账户日志
            if($log)db('user_account')->insertAll($log);  

            // 提交事务
            Db::commit();
            echo "success\n";

        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            echo "fail\n";
        }
        
    }





}
